package cn.yunyichina.provider.finance.entity;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.yunyichina.provider.finance.creator.journal.SurgeryOrderJournal;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.common.Response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 记账推荐人信息
 * 
 * @author zhou
 *
 */
public class IntroducerInfoBo {
	
	private static final Logger logger = LoggerFactory.getLogger(IntroducerInfoBo.class);

	private String userId;

	private String firstIntroducerId;
	private String firstIntroducerName;
	private String firstIntroducerOrgId;
	private String firstIntroducerOrgCode;
	private String firstIntroducerOrgName;
	private String firstIntroducerDeptId;
	private String firstIntroducerDeptCode;
	private String firstIntroducerDeptName;
	private float firstRate;

	private String secondIntroducerId;
	private String secondIntroducerName;
	private String secondIntroducerOrgId;
	private String secondIntroducerOrgCode;
	private String secondIntroducerOrgName;
	private String secondIntroducerDeptId;
	private String secondIntroducerDeptCode;
	private String secondIntroducerDeptName;
	private float secondRate;

	/**
	 * 初始化userId的推荐人信息
	 * 
	 * @param userId
	 * @return
	 */
	public IntroducerInfoBo initWithUserId(String userId) throws Exception {
		if (StringUtils.isBlank(userId)) {
			return this;
		}
		
		this.userId = userId;
		
		// 查询推荐人
		Response introducerResponse = BaseService.callHumpFromInternal(
				"yyt.base.orguser.introducer.message.get", "{\"user_id\":\""
						+ userId + "\"}");
		// 推荐人分成
		Response rateResponse = BaseService.callHumpFromInternal(
				"yyt.base.introducer.settlementsetting.get", "{\"user_id\":\""
						+ userId + "\"}");
		
		String introducerResult = introducerResponse.getResult();
		String rateResult = rateResponse.getResult();
		logger.info("推荐人信息 userId: " + userId + "，推荐人 introducer: " + introducerResult + "，分成比率 rate: " + rateResult);
		if (StringUtils.isNotBlank(introducerResult) && StringUtils.isNotBlank(rateResult)) {
			JSONObject introducerMap = JSON.parseObject(introducerResult);
			String firstIntroducerId = introducerMap.getString("firstIntroducerId");
			String firstIntroducerName = introducerMap.getString("firstIntroducerName");
			String firstIntroducerOrgId=introducerMap.getString("firstOrgId");
			String firstIntroducerOrgCode = introducerMap.getString("firstOrgCode");
			String firstIntroducerOrgName = introducerMap.getString("firstOrgName");
			String firstIntroducerDeptId = introducerMap.getString("firstDeptId");
			String firstIntroducerDeptCode = introducerMap.getString("firstDeptCode");
			String firstIntroducerDeptName = introducerMap.getString("firstDeptName");

			String secondIntroducerId = introducerMap.getString("secondIntroducerId");
			String secondIntroducerName = introducerMap.getString("secondIntroducerName");
			String secondIntroducerOrgId = introducerMap.getString("secondOrgId");
			String secondIntroducerOrgCode = introducerMap.getString("secondOrgCode");
			String secondIntroducerOrgName = introducerMap.getString("secondOrgName");
			String secondIntroducerDeptId = introducerMap.getString("secondDeptId");
			String secondIntroducerDeptCode = introducerMap.getString("secondDeptCode");
			String secondIntroducerDeptName = introducerMap.getString("secondDeptName");
			
			JSONObject rateMap = JSON.parseObject(rateResult);
			float firstRate = rateMap.getFloatValue("firstRate");
			float secondRate = rateMap.getFloatValue("secondRate");
			
			if (StringUtils.isNotBlank(firstIntroducerId)) {
				// 有一级推荐人
				this.firstIntroducerId = firstIntroducerId;
				this.firstIntroducerName = firstIntroducerName;
				this.firstIntroducerOrgId = firstIntroducerOrgId;
				this.firstIntroducerOrgCode = firstIntroducerOrgCode;
				this.firstIntroducerOrgName = firstIntroducerOrgName;
				this.firstIntroducerDeptId = firstIntroducerDeptId;
				this.firstIntroducerDeptCode = firstIntroducerDeptCode;
				this.firstIntroducerDeptName = firstIntroducerDeptName;
				this.firstRate = firstRate;
				
				if (StringUtils.isNotBlank(secondIntroducerId)) {
					this.secondIntroducerId = secondIntroducerId;
					this.secondIntroducerName = secondIntroducerName;
					this.secondIntroducerOrgId = secondIntroducerOrgId;
					this.secondIntroducerOrgCode = secondIntroducerOrgCode;
					this.secondIntroducerOrgName = secondIntroducerOrgName;
					this.secondIntroducerDeptId = secondIntroducerDeptId;
					this.secondIntroducerDeptCode = secondIntroducerDeptCode;
					this.secondIntroducerDeptName = secondIntroducerDeptName;
					this.secondRate = secondRate;
				}
			} else {
				logger.info("未查询到推荐人信息 userId: " + userId);
			}
		} else {
			logger.info("未查询到推荐人信息 userId: " + userId);
		}
		
		return this;
	}

	public String getUserId() {
		return userId;
	}

	public String getFirstIntroducerId() {
		return firstIntroducerId;
	}

	public String getFirstIntroducerName() {
		return firstIntroducerName;
	}

	public String getFirstIntroducerOrgId() {
		return firstIntroducerOrgId;
	}
	
	public float getFirstRate() {
		return firstRate;
	}

	public String getSecondIntroducerId() {
		return secondIntroducerId;
	}

	public String getSecondIntroducerName() {
		return secondIntroducerName;
	}

	public float getSecondRate() {
		return secondRate;
	}
	
	public String getFirstIntroducerOrgCode() {
		return firstIntroducerOrgCode;
	}

	public String getFirstIntroducerOrgName() {
		return firstIntroducerOrgName;
	}

	public String getFirstIntroducerDeptId() {
		return firstIntroducerDeptId;
	}

	public String getFirstIntroducerDeptCode() {
		return firstIntroducerDeptCode;
	}

	public String getFirstIntroducerDeptName() {
		return firstIntroducerDeptName;
	}

	public String getSecondIntroducerOrgId() {
		return secondIntroducerOrgId;
	}

	public String getSecondIntroducerOrgCode() {
		return secondIntroducerOrgCode;
	}

	public String getSecondIntroducerOrgName() {
		return secondIntroducerOrgName;
	}

	public String getSecondIntroducerDeptId() {
		return secondIntroducerDeptId;
	}

	public String getSecondIntroducerDeptCode() {
		return secondIntroducerDeptCode;
	}

	public String getSecondIntroducerDeptName() {
		return secondIntroducerDeptName;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
	
}
